<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="培训名称" prop="tId">
        <el-input
          v-model="queryParams.tId"
          placeholder="请输入培训名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="申请者" prop="uId">
        <el-input
          v-model="queryParams.uId"
          placeholder="请输入申请者名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->

      <el-autocomplete
        popper-class="my-autocomplete"
        v-model="state"
        :fetch-suggestions="querySearch"
        placeholder="请输入内容"
        @select="handleSelect">
        <template slot-scope="{ item }">
          <div class="name">{{ item.deptName }}</div>
          <span class="addr">{{ item.uName }}</span>
        </template>
      </el-autocomplete>

<!--      <el-autocomplete
        popper-class="my-autocomplete"
        v-model="queryParams.uId"
        :fetch-suggestions="querySearch"
        placeholder="请输入申请者名称"
        @select="handleSelect">
        <i
          class="el-icon-edit el-input__icon"
          slot="suffix"
          @click="handleIconClick">
        </i>
        <template slot-scope="{ item }">
          <div class="name">{{ item.value }}</div>
          <span class="addr">{{ item.address }}</span>
        </template>
      </el-autocomplete>-->


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="recordList" >
      <el-table-column label="序号" align="center" prop="index" />
      <el-table-column label="培训名称" align="center" prop="tName" />
      <el-table-column label="申请人所属部门" align="center" prop="deptName" />
      <el-table-column label="申请人" align="center" prop="uName" />
      <el-table-column label="申请培训" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope" >
          <div v-if="scope.row.trainRecord.applyStatus ==0 || scope.row.trainRecord.applyStatus == null">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="trailTrain(scope.row.trainRecord.id,1)"
              v-hasPermi="['train:record:edit']"
            >通过</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="trailTrain(scope.row.trainRecord.id,2)"
              v-hasPermi="['train:record:remove']"
            >拒绝</el-button>
          </div>
          <div v-else-if="scope.row.trainRecord.applyStatus ==1 ">
            <el-button type="success"  plain disabled >审核通过</el-button>
          </div>
          <div v-else>
            <el-button type="warning" plain disabled >审核拒绝</el-button>
          </div>
        </template>
      </el-table-column>
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['train:record:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['train:record:remove']"
          >删除</el-button>
        </template>
      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { listRecord, getRecord, delRecord, addRecord, updateRecord,listTrailDetail } from "@/api/train/record";

export default {
  name: "Record",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 培训人员表格数据
      recordList: [],
      recordList2: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //搜索框
      state:'',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        uId: null,
        tId: null,
        grade: null,
        report: null,
        applyStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.recordList2 = this.recordList
  },
  mounted() {
    this.recordList2 = this.recordList
  },
  methods: {
    /** 查询培训人员列表 */
    getList() {
      this.loading = true;
      listTrailDetail(this.queryParams).then(response => {
        this.recordList = response.rows;
        this.total = response.total;
        this.loading = false;
        // 使用 Array.map() 添加索引序号
        this.recordList = this.recordList.map((record, index) => {
          // 添加名为 "index" 的属性，其值为索引序号
          return { ...record, index: index + 1 };
        });
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        uId: null,
        tId: null,
        grade: null,
        report: null,
        applyStatus: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 审核按钮操作 */
    trailTrain(id,status) {
      this.form.id = id;
      this.form.applyStatus = status;
      updateRecord(this.form).then(response => {
        this.open = false;
        this.getList();
        if (status=1){
          this.$modal.msgSuccess("审核通过");
        }else {
          this.$modal.msgSuccess("审核拒绝");
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('train/record/export', {
        ...this.queryParams
      }, `record_${new Date().getTime()}.xlsx`)
    },

    querySearch(queryString, cb) {
      var recordList2 = this.recordList2;
      var results = queryString ? recordList2.filter(this.createFilter(queryString)) : recordList2;
      // 调用 callback 返回建议列表的数据
      cb(results);
    },
    createFilter(queryString) {
      return (recordList2) => {
        return (recordList2.uName.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
      };
    },

    handleSelect(item) {
      this.queryParams.uId = item.trainRecord.uId
      console.log(item);
    },

  },

};
</script>

